<template>
  <div>
    <el-row
      type="flex"
      align="middle"
    >
      <el-col
        :span="24"
        align="right"
        v-if="fromdata.status == '新建工单'"
      >
        <el-button
          type="success"
          @click="qrcEv"
          v-if="permission(configOption.authority.imprest)"
        >工单预收款</el-button>
        <el-button
          type="success"
          v-if="permission(configOption.authority.dispatch)"
          @click="rapidDispatchEv"
        >工单派单</el-button>
        <el-button type="primary">打印出门条</el-button>
        <el-button type="danger" @click="print('workOrder')">打印工单</el-button>
      </el-col>
      <el-col
        :span="24"
        align="right"
        v-else
      >
        <template v-if="configOption.authority">
          <el-button
            type="danger"
            v-if="fromdata.payStatus!='未付款'&&permission(configOption.authority.ticketRefund)"
            @click="receivePayment(1)"
          >工单退款</el-button>
          <!--修复人：杨明超 修改bug #404  所有工单详情中 退款按钮只有在付款之后才显示 -->
          <el-button
            type="success"
            v-if="permission(configOption.authority.workOrderCollection)"
            @click="receivePayment(2)"
          >工单收款</el-button>
          <el-button
            type="primary"
            @click="workAddItem"
            v-if="fromdata.status=='作业完成'&&permission(configOption.authority.workOrderRework)"
          >工单增项</el-button>
          <!--修复人：杨明超 修改bug #394  所有工单详情中，增项：只在作业完成后，工单完成前显示 -->
          <el-button
            type="primary"
            v-if="permission(configOption.authority.imprest)"
            @click="workRework"
          >工单返工</el-button>
          <el-button type="primary">打印出门条</el-button>
          <el-button type="danger" @click="print('workOrder')">打印工单</el-button>
          <el-button type="danger" @click="print">打印结算单</el-button>
        </template>
      </el-col>
    </el-row>
    <el-dialog
      v-if="visible"
      :title="dialogTitle"
      :visible.sync="visible"
      :width="width"
      :append-to-body="true"
      :close-on-click-modal="false"
      @close="dialogClose"
    >
      <PrepaidFee
        :workOrderNo="fromdata.workOrderCode"
        v-if="dialogTitle === '工单收款'"
        @dialogCloseEv="dialogCloseEv"
        :fromdata="fromdata"
      />
      <WorkRework
        @reworkResult="dialogClose"
        :workOrderId="workOrderId"
        :status="fromdata.status"
        v-if="dialogTitle === '工单返工'"
      />
      <WorkAddItem
        @orderInsProjectResult="dialogClose"
        :workOrderId="workOrderId"
        v-if="dialogTitle === '工单增项'"
      />
    </el-dialog>
  </div>
</template>

<script>
import { Steps, QRCode } from "@/components/index";
import WorkRework from "./WorkRework";
import WorkAddItem from "./WorkAddItem";
import PrepaidFee from './PrepaidFee';
import { permission,exportFile } from '@/utils/index'
import {orderTypeData} from '../config/index';
export default {
  components: { Steps, QRCode, WorkRework, WorkAddItem, PrepaidFee },
  props: {
    fromdata: {
      type: Object,
      default: () => ({}),
    },
    workOrderId: {
      type: Number | String,
    },
    workOrderType: {
      type: Number | String,
      default: 1, //默认快速单子
    },
  },
  data () {
    return {
      visible: false,
      dialogTitle: "",
      width: "",
      configOption: {},
      baseUrl: '',
      dialogCloseEv: '',
    };
  },
  methods: {
    async print(keys){
      let orderType=orderTypeData.filter(item=>item.key==this.workOrderType)
      switch (keys) {
        case 'workOrder':
          exportFile(`${process.env.BASE_API}/order/print/${this.workOrderId}/${orderType[0].value}`,`${orderType[0].value}.pdf`);
          break;
      
        default:
          break;
      }
      
    },
    permission,
    //二维码首款
    async qrcEv () {
      this.visible = true;
      this.dialogTitle = "工单收款";
      this.width = "400px";
    },
    dialogClose () {
      this.visible = false;
    },
    //获取父级配置
    getParentConfigOption (dom, actionName) {
      if (`${actionName}` in dom) {
        return dom[`${actionName}`];
      } else if ("$parent" in dom) {
        return this.getParentConfigOption(dom.$parent, actionName);
      } else {
        return;
      }
    },
    async rapidDispatchEv () {

      let fun = this.configOption.orderDispatch;
      if (fun) {
        let option = await fun(this.workOrderId);
        if (option.success && !option.data) {
          this.$message.success("操作成功");

          if (this.dialogCloseEv) {
            this.dialogCloseEv();
          } else {
            this.$message.error('从该工单列表上未找到相应关闭方法(dialogCloseEv),关闭弹窗以及刷新失败')
          }
        } else {
          this.$message.error(option.data);
        }
      }else{
        console.error('获取order配置失败',this.configOption,this.getParentConfigOption(this.$parent, 'getConfig'))
      }
    },
    // 工单返工
    workRework () {
      this.visible = true;
      this.dialogTitle = "工单返工";
      this.width = "1000px";
    },
    // 工单增项
    workAddItem () {
      this.visible = true;
      this.dialogTitle = "工单增项";
      this.width = "1000px";
    },
    changeType (item) {
      this.selectedTypeText = item.label;
    },
    //工单收款或者退款
    receivePayment (type) {//1 退款 2收款
      this.$router.push({
        path: "/workOrderManage/settlement",
        query: { id: this.workOrderId, type: this.workOrderType, settlemenType: type },
      });
    },
  },
  created () {
    this.configOption = this.getParentConfigOption(this.$parent, 'getConfig');
    this.dialogCloseEv = this.getParentConfigOption(this.$parent, 'dialogCloseEv');
  },
  mounted () {
    this.$nextTick(() => {
      this.configOption = this.getParentConfigOption(this.$parent, 'getConfig');
    })
  }
};
</script>

<style lang="scss" scoped>
.el-table .delete-row {
  background: #f5f7fa;
  color: #ccc !important;
}
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 80px;
  height: 80px;
  display: block;
}
.colorF40 {
  color: #f40;
}
</style>
